package com.example.testrxjava;

import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.os.Bundle;
import android.view.MenuItem;

import com.example.testrxjava.base.BaseActivity;
import com.example.testrxjava.fragment.code.WxArticleFragment;
import com.example.testrxjava.fragment.home.HomeFragment;
import com.example.testrxjava.fragment.system.KnowledgeFragment;
import com.example.testrxjava.fragment.person.PersonFragment;
import com.example.testrxjava.fragment.project.ProjectFragment;
import com.example.testrxjava.util.CustomViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class IndexActivity extends BaseActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
    @BindView(R.id.index_viewPager)
    CustomViewPager viewPager;
    @BindView(R.id.BottomBnv)
    BottomNavigationView bottomNavigationView;
    private List<Fragment> data;

    @Override
    public int getLayoutId() {
        return R.layout.activity_index;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }

    @Override
    public void init(Bundle savedInstanceState) {
        data=new ArrayList<Fragment>();

        data.add(new HomeFragment());
        data.add(new KnowledgeFragment());
        data.add(new WxArticleFragment());
        data.add(new ProjectFragment());
        data.add(new PersonFragment());
        viewPager.setScanScroll(false);
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                switch (i){
                    case 0:
                        return data.get(0);
                    case 1:
                        return data.get(1);
                    case 2:
                        return data.get(2);
                    case 3:
                        return data.get(3);
                    case 4:
                        return data.get(4);

                }


                return null;
            }

            @Override
            public int getCount() {
                return data.size();
            }
        });


            initLinstener();
    }

    @Override
    public void initLinstener() {
//        viewPager.addOnPageChangeListener(this);
        bottomNavigationView.setOnNavigationItemSelectedListener(this);
    }
/**
    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        bottomNavigationView.getMenu().getItem(i).setChecked(true);
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }
*/
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        switch (menuItem.getItemId()){
            case R.id.homeItem:
                viewPager.setCurrentItem(0);
                break;
            case R.id.systemItem:
                viewPager.setCurrentItem(1);
                break;
            case R.id.wxItem:
                viewPager.setCurrentItem(2);
                break;
            case R.id.projectItem:
                viewPager.setCurrentItem(3);
                break;
            case R.id.personItem:
                viewPager.setCurrentItem(4);
                break;

        }
        return true;
    }
}
